package com.teamin.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.teamin.config.ErrorCode;
import com.teamin.entity.CommentEntity;
import com.teamin.entity.MessageEntity;
import com.teamin.security.AESUtils;
import com.teamin.service.MessageService;
import com.teamin.service.impl.wx.SmallMessageManager;
import com.teamin.service.impl.wx.WXSendMessage;
import com.teamin.web.exception.GlobalErrorInfoException;
import com.teamin.web.model.ErrorInfo;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/v1")
@Api(description = "用户消息相关接口",tags = "消息")
public class MessageController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private MessageService messageService;
	PodamFactory factory = new PodamFactoryImpl();

	// ResponseEntity
	/**
	 * 删除消息
	 * @return
	 * @throws GlobalErrorInfoException 
	 * @throws Exception
	 */
	@RequestMapping(value = "/messages/{messageId}", method = RequestMethod.DELETE)
	public String deleteMessage(@PathVariable("messageId") Long messageId) throws GlobalErrorInfoException {
		if (true)
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.UNFINISH_METHORD));
		return "success";
	}

	/**
	 * 查看消息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/messages/{messageId}", method = RequestMethod.GET)
	public MessageEntity getMessage(@PathVariable("messageId") Long messageId) throws Exception {
		if (true)
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.UNFINISH_METHORD));
		return factory.manufacturePojo(MessageEntity.class);
	}

	/**
	 * 设置一条消息已读消息
	 * @return
	 */
	@RequestMapping(value = "/messages/{messageId}", method = RequestMethod.PUT)
	public String setMessageRead(@PathVariable("messageId") Long messageId,@RequestParam(required = true) Integer read,@RequestAttribute("loginUserId") Long loginUserId,
								 @RequestAttribute("userFlag") String userFlag) throws Exception {
		MessageEntity messageEntity = messageService.selectByPrimaryKey(messageId);
		if (messageEntity.getUserId() != loginUserId) {
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.FORBIDDEN_UPDATE_MESSAGE));
		}
		if (read != null && read == 1) {
			messageService.setMessageReaded(loginUserId,""+messageId,userFlag);
		}		
		return "success";
	}

	/**
	 * 设置消息页面已查看
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/messages", method = RequestMethod.PUT)
	public MessageEntity setMessagesRead(int read) throws Exception {

		return factory.manufacturePojo(MessageEntity.class);
	}

	/**
	 * 发送小程序卡片消息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/messages/action/sendcardmessage", method = RequestMethod.POST)
	public void sendWXSmallCardMessage(HttpServletRequest request) throws Exception {
		JSONObject jsonEncrypt = JSONObject.parseObject(AESUtils.decrypt(getInputData(request.getInputStream()), SmallMessageManager.SMALL_CARD));
		SmallMessageManager.sendTemplateMessage(jsonEncrypt);
	}

	/**
	 * 发送公众号卡片消息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/messages/action/sendwechatmpcardmessage", method = RequestMethod.POST)
	public void sendWechatMPCardMessage(HttpServletRequest request) throws Exception {
		JSONObject jsonEncrypt = JSONObject.parseObject(AESUtils.decrypt(getInputData(request.getInputStream()), WXSendMessage.SEND_MESSAGE_WX_PASSWORD));
		WXSendMessage.getInstance().postMessage(jsonEncrypt);
	}

	/**
	 * 删除消息,或设为消息已读,用户的所有消息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/message/action/setread", method = RequestMethod.PUT)
	@ApiOperation(value = "将个人的消息设为已读状态")
	@ApiResponse(code = 200,message = "删除成功")
	public void updateMessageRead(@ApiParam(hidden = true) @RequestAttribute("loginUserId") Long loginUserId,
								  @ApiParam("要设置的消息ID 用逗号分隔,如为空则表示将用户所有消息都设为已读")
								  @RequestParam String messageIds,
								  @RequestAttribute("userFlag") String userFlag) throws Exception {
		messageService.setMessageReaded(loginUserId,messageIds,userFlag) ;
	}

	/**
	 * 删除消息,或设为消息已读
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/message/{messageId}/action/setread", method = RequestMethod.PUT)
	public void updateMessageRead(@PathVariable("messageId") Long messageId,
								  @RequestAttribute("loginUserId") Long loginUserId,String messageIds,
								  @RequestAttribute("userFlag") String userFlag) throws Exception {
		messageService.setMessageReaded(loginUserId,""+messageId,userFlag) ;
	}

}